﻿using System.Security.Cryptography.X509Certificates;
using _10GrpcServer;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using GreeterClient = _10GrpcServer.Greeter.GreeterClient;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpcClient<GreeterClient>(x => { x.Address = new Uri("https://localhost:5001"); })
	.ConfigureChannel(x =>
	{
		x.HttpHandler = CreateHttpHandler();
	});

var app = builder.Build();

using var scope = app.Services.CreateScope();
var client = scope.ServiceProvider.GetRequiredService<GreeterClient>();
var reply = await client.SayHelloAsync(new HelloRequest { Name = "Grpc" });
Console.WriteLine(reply.Message);

static HttpClientHandler CreateHttpHandler()
{
	var handler = new HttpClientHandler();
	var clientCertificate = X509CertificateLoader.LoadPkcs12FromFile("Certs/hardstyle.pfx", "pass1234");
	handler.ClientCertificates.Add(clientCertificate);
	return handler;
}
